package com.klb.action.catalog;

import com.klb.model.BbsArticle;
import com.klb.service.catalog.impl.SingleServiceCatalogImpl;
import com.klb.tools.PostValue;
import com.klb.tools.Sessions;
import com.klb.tools.Tools;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fujia on 2016/6/6.
 */
public class SingleActionCatalog extends ActionSupport {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String comment;
    private int userId;
    private int blockId;
    
    private HashMap<String,Object> data;
    private int article;
    
    private SingleServiceCatalogImpl singleServiceCatalog;


    public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
	}

	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getBlockId() {
		return blockId;
	}
	public void setBlockId(int blockId) {
		this.blockId = blockId;
	}


	public void setSingleServiceCatalog(SingleServiceCatalogImpl singleServiceCatalog) {
		this.singleServiceCatalog = singleServiceCatalog;
	}

	@Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();

        if (!Tools.objectIsNullOrNot(request.getParameter("article"))){
            int articleId = Integer.parseInt(request.getParameter("article"));

            BbsArticle article = this.singleServiceCatalog.getArticleById(articleId);
            String blockName = this.singleServiceCatalog.getBlockNameByArticle(article);
            List<HashMap<String, Object>> replys = this.singleServiceCatalog.packReplyByArticleId(articleId);

            Sessions.setSession("single", article);
            Sessions.setSession("blockName", blockName);
            Sessions.setSession("replys", replys);
        }

        return SUCCESS;
    }

    public String jsonForSingle(){
        JSONObject jsonObject = PostValue.getJsonObject();

        if (!Tools.objectIsNullOrNot(jsonObject)){
            int articleId = (int) jsonObject.get("articleId");

            BbsArticle article = this.singleServiceCatalog.getArticleById(articleId);
            String blockName = this.singleServiceCatalog.getBlockNameByArticle(article);
            List<HashMap<String, Object>> replys = this.singleServiceCatalog.packReplyByArticleId(articleId);

            this.data = new HashMap<>();

            this.data.put("article", article);
            this.data.put("blockName", blockName);
            this.data.put("replys", replys);
            this.data.put("success", true);
        } else {
            this.data = null;
        }

        return SUCCESS;
    }

    public Map<String, Object> getData() {
        return data;
    }


    public String postComment(){
        if (this.singleServiceCatalog.addReplyIsSuccess(this.userId, this.blockId, this.comment))
            return SUCCESS;

        return ERROR;
    }

    public int getArticle() {
        HttpServletRequest request = ServletActionContext.getRequest();
        if (!Tools.objectIsNullOrNot(request.getParameter("article"))){
            this.article = Integer.parseInt(request.getParameter("article"));
        } else {
            this.article = 0;
        }

        return this.article;
    }
}
